var skill = {
	save: function() {
		delete this.user_info;
		db.update(db_table_name, this._id, this, true);
	},
	//商家销毁市代
	destroy: function() {
		if (this.MA_id) { // 间接属于市代的门店
			var MA = BusMACon.get(this.MA_id);
			MA && MA._removeShop(this._id);
		} else { // 直接属于商家的门店
			var bus = BusCon.get(this.bus_id);
			bus._removeShop(this._id);
		}
		db.remove(db_table_name, this._id)
	},

	setInfo: function(info, is_no_save) {
		if (info.hasOwnProperty("state") && BusShopCon.STATE.hasOwnValue(info.state = parseInt(info.state))) {
			this.state = info.state;
			// 收益只发生在下订单的时候
/*			// 介绍市代的返利
			if (this.state == BusShopCon.STATE.审核通过 &&
				this.introducer_id &&
				!this._rebate_for_introducer) {

				// var bus_user = BusUserCon.build({
				// 	bus_id: this.bus_id,
				// 	user_id: this.introducer_id
				// }, "get_instance");
				// bus_user._addMoney();
			}*/
		}
		//经度
		if (!info.hasOwnProperty("lng") && false) { // 暂时不做严格校验
			throwCusError("经度不可为空");
		}
		this.lng = parseFloat(info.lng);
		if (isNaN(this.lng)) {
			this.lng = 0;
			// throwCusError("经度格式错误"); 
		}
		if (-180 > this.lng || this.lng > 180) {
			throwCusError("经度范围有误");
		}
		//纬度
		if (!info.hasOwnProperty("lat") && false) { // 暂时不做严格校验
			throwCusError("纬度不可为空");
		}
		this.lat = parseFloat(info.lat);
		if (isNaN(this.lat)) {
			this.lat = 0;
			// throwCusError("纬度格式错误"); 
		}
		if (-90 > this.lat || this.lat > 90) {
			throwCusError("纬度范围有误");
		}

		// 联系人
		if (info.hasOwnProperty("contacts") && (info.contacts = info.contacts.trim())) {
			this.contacts = info.contacts;
		}
		// 手机号
		if (info.hasOwnProperty("mobile_phone") && (info.mobile_phone = info.mobile_phone.trim())) {
			if (!helper.phone(info.mobile_phone)) {
				throwCusError("门店手机号码有误")
			}
			this.mobile_phone = info.mobile_phone;
		}
		// 邮箱
		if (info.hasOwnProperty("email") && (info.email = info.email.trim())) {
			if (!helper.isEmail(info.email)) {
				throwCusError("门店邮箱有误")
			}
			this.email = info.email;
		}
		// QQ号码
		if (info.hasOwnProperty("qq") && (info.qq = info.qq.trim())) {
			this.qq = info.qq;
		}
		// 店名
		if (info.hasOwnProperty("store_name") && (info.store_name = info.store_name.trim())) {
			this.store_name = info.store_name;
		} else {
			throwCusError("店铺名不可为空");
		}
		// 开业时间
		if (info.hasOwnProperty("start_business_hours") && (info.start_business_hours = info.start_business_hours.trim())) {
			this.start_business_hours = info.start_business_hours;
		}
		// 打样时间
		if (info.hasOwnProperty("end_business_hours") && (info.end_business_hours = info.end_business_hours.trim())) {
			this.end_business_hours = info.end_business_hours;
		}
		// 区号
		if (info.hasOwnProperty("area_code") && (info.area_code = info.area_code.trim())) {
			this.area_code = info.area_code;
		}
		// 座机号码
		if (info.hasOwnProperty("phone") && (info.phone = info.phone.trim())) {
			this.phone = info.phone;
		}
		// 商标
		if (info.hasOwnProperty("logo_img")) {
			if (helper.isUrl(info.logo_img)) {
				throwCusError("门店商标图片地址有误")
			}
			this.logo_img = info.logo_img;
		}
		// 地址String
		if (info.hasOwnProperty("adress") && (info.adress = info.adress.trim())) {
			this.adress = info.adress;
		}
		// 门店图片Array
		if (info.hasOwnProperty("store_imgs")) {
			info.store_imgs = info.store_imgs.filter(img_url => typeof img_url === "string" && helper.isUrl(img_url));
			this.store_imgs = info.store_imgs;
		}

		is_no_save || this.save();
	}
};

module.exports = skill.multiInherits({});

var db = require("../db/index");
var UserCon = require("../user/_con");
var BusCon = require("../bus/_con");
var GoodsCon = require("../goods/_con");
var AchCon = require("../ach/_con");
var BusMACon = require("../bus.MA/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusShopCon = require("../bus.shop/_con");
var BuffCon = require("../buff/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var $Array = require("../_include/$.Array");
var $Object = require("../_include/$.Object");
var helper = require("../_include/helper");
var db_table_name = config.dbname.busShop;
var db_id_prefix = config.dbIdPrefix.busShop;;